package com.niit.users.action;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.niit.common.pojo.ApiResponse;
import com.niit.common.pojo.ApiResponseEnum;
import com.niit.common.util.ConstatFinalUtil;
import com.niit.common.util.PageInfoUtil;
import com.niit.users.pojo.TbBus;
import com.niit.users.pojo.TbBusTimeable;
import com.niit.users.pojo.TbOrder;
import com.niit.users.pojo.TbPassenger;
import com.niit.users.service.IBusDbService;
import com.niit.users.service.IUsersDbService;
import com.niit.users.service.impl.BusDbServiceImpl;
import com.niit.users.service.impl.UsersDbServiceImpl;

/**
 * 车辆相关的Action
 */
public class BusAction {
    /**
     * Bus相关的Service
     */
    private IBusDbService busDbService = new BusDbServiceImpl();
    private IUsersDbService usersDbService = new UsersDbServiceImpl();

    /**
     * 添加巴士
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> saveOneBusService(TbBus pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        if (Objects.isNull(pojo)) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        if (Objects.nonNull(pojo) && "".equalsIgnoreCase(pojo.getBusLicenseNo())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        apiResponse = this.busDbService.saveOneBusService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 更新巴士
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> updateOneBusService(TbBus pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        apiResponse = this.busDbService.updateOneBusService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 删除巴士
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<Object> deleteOneBusService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = this.busDbService.deleteOneBusService(condMap);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 查询巴士列表
     * 
     * @param pageInfoUtil 为空表示不分页
     * @param condMap
     * @return
     */
    public ApiResponse<TbBus> findCondListBusService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbBus> apiResponse = this.busDbService.findCondListBusService(pageInfoUtil, condMap);
        TbBus pojoUtil = new TbBus();
        pojoUtil.printTitle();
        List<TbBus> dataList = apiResponse.getDataListJava();
        /* 打印标题 */
        for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
            TbBus dataTemp = (TbBus) iterator.next();
            dataTemp.printInfo();
        }
        return apiResponse;
    }

    /**
     * 查询巴士单条记录
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<TbBus> findOneBusService(Map<String, Object> condMap) {
        ApiResponse<TbBus> apiResponse = this.busDbService.findOneBusService(condMap);
        TbBus dataTemp = apiResponse.getDataOneJava();
        if (Objects.nonNull(dataTemp)) {
            TbBus pojoUtil = new TbBus();
            pojoUtil.printTitle();
            dataTemp.printInfo();
        }
        return apiResponse;
    }
    
    /**
     * 添加班次
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> saveOneBusTimeableService(TbBusTimeable pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        if (Objects.isNull(pojo)) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        if (Objects.nonNull(pojo) && "".equalsIgnoreCase(pojo.getDepartureStation())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        /* 查询巴士信息 */
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.put("id", pojo.getBusId());
        ApiResponse<TbBus> busDbApiResponse = this.busDbService.findOneBusService(condMap);
        TbBus busDb = busDbApiResponse.getDataOneJava();
        if (Objects.isNull(busDb)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RELA_DATA_NOT_EXISTS.getStatus());
            Map<String,String> infoMap = new HashMap<String, String>();
            infoMap.put("relaData", "巴士信息");
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        apiResponse = this.busDbService.saveOneBusTimeableService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 更新班次
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> updateOneBusTimeableService(TbBusTimeable pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 查询巴士信息 */
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.put("id", pojo.getBusId());
        ApiResponse<TbBus> busDbApiResponse = this.busDbService.findOneBusService(condMap);
        TbBus busDb = busDbApiResponse.getDataOneJava();
        if (Objects.isNull(busDb)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RELA_DATA_NOT_EXISTS.getStatus());
            Map<String,String> infoMap = new HashMap<String, String>();
            infoMap.put("relaData", "巴士信息");
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        apiResponse = this.busDbService.updateOneBusTimeableService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 删除班次
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<Object> deleteOneBusTimeableService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = this.busDbService.deleteOneBusTimeableService(condMap);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 查询班次列表
     * 
     * @param pageInfoUtil 为空表示不分页
     * @param condMap
     * @return
     */
    public ApiResponse<TbBusTimeable> findCondListBusTimeableService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbBusTimeable> apiResponse = this.busDbService.findCondListBusTimeableService(pageInfoUtil, condMap);
        TbBusTimeable pojoUtil = new TbBusTimeable();
        pojoUtil.printTitle();
        List<TbBusTimeable> dataList = apiResponse.getDataListJava();
        /* 打印标题 */
        for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
            TbBusTimeable dataTemp = (TbBusTimeable) iterator.next();
            dataTemp.printInfo();
        }
        return apiResponse;
    }

    /**
     * 查询班次单条记录
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<TbBusTimeable> findOneBusTimeableService(Map<String, Object> condMap) {
        ApiResponse<TbBusTimeable> apiResponse = this.busDbService.findOneBusTimeableService(condMap);
        TbBusTimeable dataTemp = apiResponse.getDataOneJava();
        if (Objects.nonNull(dataTemp)) {
            TbBusTimeable pojoUtil = new TbBusTimeable();
            pojoUtil.printTitle();
            dataTemp.printInfo();
        }
        return apiResponse;
    }
    
    /**
     * 添加订单
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> saveOneOrderService(TbOrder pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        if (Objects.isNull(pojo)) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        if (Objects.nonNull(pojo) && "".equalsIgnoreCase(pojo.getRideDate())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.put("id", pojo.getBusId());
        ApiResponse<TbBus> busDbApiResponse = this.busDbService.findOneBusService(condMap);
        TbBus busDb = busDbApiResponse.getDataOneJava();
        if (Objects.isNull(busDb)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RELA_DATA_NOT_EXISTS.getStatus());
            Map<String,String> infoMap = new HashMap<String, String>();
            infoMap.put("relaData", "巴士信息");
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        condMap.put("id", pojo.getTimetableId());
        ApiResponse<TbBusTimeable> timeableDbApiResponse = this.busDbService.findOneBusTimeableService(condMap);
        TbBusTimeable timeableDbObj = timeableDbApiResponse.getDataOneJava();
        if (Objects.isNull(timeableDbObj)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RELA_DATA_NOT_EXISTS.getStatus());
            Map<String,String> infoMap = new HashMap<String, String>();
            infoMap.put("relaData", "班次信息");
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        condMap.put("id", pojo.getPassengerId());
        ApiResponse<TbPassenger> passDbApiResponse = this.usersDbService.findOnePassengerService(condMap);
        TbPassenger passDbObj = passDbApiResponse.getDataOneJava();
        if (Objects.isNull(passDbObj)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RELA_DATA_NOT_EXISTS.getStatus());
            Map<String,String> infoMap = new HashMap<String, String>();
            infoMap.put("relaData", "乘客信息");
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                    Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        apiResponse = this.busDbService.saveOneOrderService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 更新订单
     * 
     * @param pojo
     * @return
     */
    public ApiResponse<Object> updateOneOrderService(TbOrder pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        apiResponse = this.busDbService.updateOneOrderService(pojo);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 删除订单
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<Object> deleteOneOrderService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = this.busDbService.deleteOneOrderService(condMap);
        System.out.println("操作结果返回:" + apiResponse.getInfo());
        return apiResponse;
    }

    /**
     * 查询订单列表
     * 
     * @param pageInfoUtil 为空表示不分页
     * @param condMap
     * @return
     */
    public ApiResponse<TbOrder> findCondListOrderService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbOrder> apiResponse = this.busDbService.findCondListOrderService(pageInfoUtil, condMap);
        TbOrder pojoUtil = new TbOrder();
        pojoUtil.printTitle();
        List<TbOrder> dataList = apiResponse.getDataListJava();
        /* 打印标题 */
        for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
            TbOrder dataTemp = (TbOrder) iterator.next();
            dataTemp.printInfo();
        }
        return apiResponse;
    }

    /**
     * 查询订单单条记录
     * 
     * @param condMap
     * @return
     */
    public ApiResponse<TbOrder> findOneOrderService(Map<String, Object> condMap) {
        ApiResponse<TbOrder> apiResponse = this.busDbService.findOneOrderService(condMap);
        TbOrder dataTemp = apiResponse.getDataOneJava();
        if (Objects.nonNull(dataTemp)) {
            TbOrder pojoUtil = new TbOrder();
            pojoUtil.printTitle();
            dataTemp.printInfo();
        }
        return apiResponse;
    }
}
